import { Button, Icon, Toast } from 'react-vant'
import { useState } from 'react'
import { followedUser, deleteFollowed } from '../api/user'
import { getToken } from '../utils/index'
export default (porps) => {
    const [followedloading, setFollowedLoading] = useState(false)
    const [is_followed, setIs_followed] = useState(porps.is_followed)
    const followedClick = async () => {
            setFollowedLoading(true)
            if (is_followed) {
                const res = await deleteFollowed(porps.aut_id)
                setIs_followed(false)
                Toast('取消关注成功')
            } else {
                try {
                    const res = await followedUser(porps.aut_id)
                    setIs_followed(true)
                    Toast('关注成功')
                } catch (error) {
                    if (error.response && error.response.status === 400) {
                        Toast('你不能关注你自己')
                    }
                }
            }
            setFollowedLoading(false)
    }
    if (is_followed) {
        return (<Button type="primary" onClick={followedClick} loading={followedloading} round>已关注</Button>)
    } else {
        return (<Button type="info" onClick={followedClick} loading={followedloading} round><Icon name="plus" />关注</Button>)
    }
}